import { _decorator, CCString, Component, Node, find } from "cc";
const { ccclass, property } = _decorator;
import { getGlobal, MyGlobal } from "../Global";
@ccclass("AppStart")
export class AppStart extends Component {
  start() { }
  @property(MyGlobal)
  g = null as MyGlobal;
  @property(CCString)
  mainScene = "";
  onLoad() {
    let g = (this.g = getGlobal());
    g.utils.setFitSreenMode(find("Canvas"));
    this.mainScene = "loading";
    g.utils.preload("login", () => {
      setTimeout(() => {
        cc.director.loadScene("login");
        this.getServerInfo();
      }, 500);
    });

  }
  getServerInfo() {
    let complete = false;
    let xhr;
    let g = (this.g = getGlobal());
    // console.log(g.SI, "?ggb")
    let timer;
    // if (!g.SI) return;
    let fn = () => {
      if (!complete) {
        if (xhr) {
          xhr.abort();
          timer = setTimeout(() => {
            fn();
          }, 5000);
        } else {
          xhr = this.g.http.sendRequest("/get_serverinfo", null, (ret) => {
            console.log(ret, "??ret");
            clearTimeout(timer);
            xhr = null;
            complete = true;
            g.SI = ret;
            cc.sys.localStorage.setItem("SI", JSON.stringify(ret));
            // cc.director.loadScene(this.mainScene)
          });
        }
      }
    };
    fn();
  }
  update(deltaTime: number) { }
}
